Skip to content

feat(agents): 资源库指令只写标记块,多套指令各自开关并可限定 Agent - #261

Merged
xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-instructions-block
Dec 3, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-instructions-block

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联

改动

都是 API-45、agents 能力 0 内的补充,不升版本。插件后端协议不变,四个 SDK 不需要改。新增错误码 agentLibrary.setExists,新增日志事件 agent.library.cleanup_failed。

一、指令文件只写标记块

  • Core 只写 <!-- lithe:begin (written by Lithe from the agent library) --> 到 <!-- lithe:end --> 之间的一块。块外的用户文字逐字节保留,只把紧挨块的空行收成一行。
  • 新块追加在用户文字之后,空一行;已有的块原地更新。文件用 \r\n 时块内也用 \r\n。
  • 首行以 <!-- lithe:begin 开头、以 --> 结尾就算数,用户改了中间的字也认;块从首行到其后第一个尾行。
  • 资源库文字里的 <!-- lithe: 写成 <!-- lithe :,不会提前结束块。
  • 指令为空时去掉整块;多余的块、落单的首行或尾行都清掉;重复写入文件不变大。
  • 文件不是 UTF-8 不写;写完会超过 1 MiB 的拒绝(fileTooLarge)。
  • state.json 改记块内文字的哈希。已有文件的迁移:以前整份写过、哈希一致的文件,下次写成只有块;其他没有块的文件都算用户文字,块追加在后面,不需要确认。
  • agentLibrary.read 的 instructions.state 改为七种:absent、noBlock、current、stale、edited、tooLarge、unreadable;新增 ownLines(块外用户文字的行数,tooLarge、unreadable 不带)。
  • 块被用户改过(edited)时,写入要 replace: true。agentLibrary.otherInstructions 收窄为“块里有不是 Lithe 写的内容”。

二、多套指令

  • 每套新增 on(read 总会给出)和 agents(最多 256 个不重复的 Agent key,空为全部);去掉 active。
  • 拼接:按 sets 顺序取开着且给该 Agent 的每一套,去掉尾部空白、跳过空的,再接该 Agent 的 extra,段间空一行,末尾一个换行。
  • 读到带 active 的已有 library.json 时转换成“那一套开、其余关”,拼出的文字逐字节一致(测试覆盖);active 指向不存在的套报 invalidLibraryFile。save 带 active 报 invalidLibrary。

三、并入块外的用户文字

  • agentLibrary.apply 新操作 {category:"instructions", op:"import", id, name?, revision, replace?}。
  • 带 name:新建一套,开着,只给这个 Agent;id 已被占用报新错误码 agentLibrary.setExists。
  • 不带 name:追加到已有那一套的末尾;已经包含同样文字就不重复加;这一套会打开,它有 Agent 列表时补上这个 Agent。
  • 两种情况该 Agent 的 enabled 都变成 true。顺序:先备份文件,再把文件写成只剩块,然后保存资源库;资源库存不下就把文件放回。
  • 被用户改过的块里的文字不算用户自己的,不会被并入;要 replace,这部分只留在备份里。

四、保存后由 Core 同步

  • agentLibrary.save 存下资源库后,按 agents.list 的顺序给每个跟随资源库的 Agent 写块,不覆盖用户改过的块。
  • 回答新增 instructions,逐个 Agent 给出 written、unchanged、skipped(原因 otherInstructions、notGranted、noInstructions、instructionsOff)或 failed(写入的拒绝码)。一个 Agent 失败不影响其他 Agent,资源库照样保存。不跟随的 Agent 不列出。
  • 宿主不再按状态挑 Agent、逐个调用写入。

五、恢复后停止跟随

  • agentProviders.restore 放回指令文件时,先忘掉写入记录,再把该 Agent 的 enabled 关掉,回答新增 unfollowed。否则下一次保存会把块写回去,等于撤销了恢复。
  • library.json 读不了时不改它,unfollowed 为 false。日志 agent.providers.restore_applied 新增必填字段 unfollowed。

六、清理失败不再静默

  • 新增日志事件 agent.library.cleanup_failed(warn),字段 target(temporary、staging、skillImport、backup、backupRecord)和 error(只记 io 错误类别,不记路径或内容,残留文件可能含配置或密钥)。
  • library.rs 和 library/backups.rs 里原来用 let _ = 丢弃的 14 处清理错误,改为经 remove_leftover 处理:目标不存在算正常收尾,其他错误记这个事件。undo 和恢复中途存回备份记录失败也记录,否则之后恢复失败时查不到原因。
  • library/providers.rs 插件修改失败后放回供应商列表那一处加 log-exempt:插件那次失败已由 agent.providers.change_failed 记录。

七、文档

对宿主的影响

桌面端在升级固定的 Core 提交前必须先改:这个 Core 拒绝带 active 的保存,read 每套带 on,状态值换成七种,保存回答带逐 Agent 的同步结果。桌面接入在 1lck/Lithe-IDEA#2654 的后续工作包里做,届时升级 PINNED_CORE_REVISION。按维护者的决定,不考虑旧版 Lithe 和旧 Core 的兼容与降级,只保留上面两项已有数据的迁移。

已知限制

  • 不包含:插件声明各 Agent 的覆盖文件或专属提示、把一套指令存在用户自己的文件里、记忆摘要。
  • 用户删掉块的标记、只留下块内文字时,如果那段文字恰好和 Lithe 上次写入的完全相同,会被当作以前整份写入的旧文件,下次写成只有块。

验证(macOS arm64 本机)

命令 结果
cargo test -p lithe-agent-runtime -p lithe-diagnostics 104 + 72 通过
cargo clippy -p lithe-agent-runtime -p lithe-diagnostics --all-targets --all-features -- -D warnings 通过
Windows 目标 clippy(x86_64-pc-windows-msvc,同上两个 crate) 通过
cargo fmt --all --check 通过
node shared/plugins/scripts/verify-docs.mjs 通过
node scripts/logging/generate-log-events.mjs --check 通过(177 个事件)
scripts/rust-core/verify-rust-core-comments.sh agent-runtime diagnostics 通过
node scripts/rust-core/verify-platform-tests.mjs 通过(55 个 crate)

新增 rust/crates/agent-runtime/src/tests/library/instructions.rs(11 个测试)和共享样例 rust/fixtures/agents/library-instructions-v1.json(12 个块布局用例和全部状态、结果取值,测试会读取它)。覆盖:块外文字不变;旧整份文件迁移;无块文件追加;块内被改后不覆盖、replace 后覆盖;标记行失效;多块与残留结束行收拾;多套按顺序和 Agent 过滤拼接;active 迁移逐字节一致;并入后文件只剩块且有备份;保存后逐 Agent 的同步结果;恢复后跟随关闭;清理失败只记一条不含路径的日志。测试没有 sleep,文件读写都在 Harness 的临时目录里。

没有跑整个 workspace:日志目录改了,CI 会选全部 crate。

- 指令文件里 Core 只写 <!-- lithe:begin … --> 到 <!-- lithe:end --> 之间的一块,块外的用户文字逐字节保留;新块追加在用户文字之后,多余的块和落单的标记行清掉,指令为空时去掉整块,重复写入不变大。
- 读取状态改为 absent/noBlock/current/stale/edited/tooLarge/unreadable,并报告块外用户文字的行数 ownLines;块被用户改过时写入要 replace。
- 指令集每套有 on 和 agents(空为全部),按顺序拼接后接 Agent 的 extra;去掉 active,读到旧文件时迁移成“该套开、其余关”,拼出的文字逐字节一致。
- 新操作 instructions/import:把块外用户文字并入新建或已有的一套,先备份,文件只剩块。
- agentLibrary.save 保存后由 Core 同步所有跟随资源库的 Agent,逐个回答 written/unchanged/skipped/failed,不覆盖用户改过的块。
- agentProviders.restore 放回指令文件时关闭该 Agent 的跟随(unfollowed),避免下次保存撤销恢复。
- 清理残留文件失败时记 agent.library.cleanup_failed,只记目标类别和错误类别;library.rs、backups.rs 不再静默丢弃清理错误。
- 协议更新记录:16 个已合入的 PR 移进时间线并回填日期与合入提交。

Refs 1lck/Lithe-IDEA#2654
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review December 3, 2026 11:44
@xiaoyumuxi
xiaoyumuxi merged commit e8ffcf7 into main Dec 3, 2026
7 of 9 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 potential issues.

Devin Review

Ok(json!({ "revision": digest(&bytes) }))
// Each agent is written under the writing lock of its own, so a
// change elsewhere never waits for every agent's file.
let instructions = self.sync_instructions(runtime, &config, &request.library, None);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Restore silently removes personal instructions

After a user adds notes outside the block, sync_instructions hides those edits from restore when it updates the block. Restore compares against the new whole-file digest, then restores the older baseline without asking. The added notes disappear from the agent’s file.

Learn more

A restore returns the file to its original baseline, not to the file just before the latest sync. Backup creation keeps the first baseline but updates the record to match the entire file after each write. write_block carries user text forward when it updates the block, so this new digest also covers user edits made since the baseline. Restore then accepts the file without discard confirmation because its digest matches. It saves the current file in backup history, but the notes are no longer in the file the agent reads.

Example: Start with My notes, enable following, and append Use the staging environment outside the block. Change a library set and save it. A normal restore now puts back only My notes without asking about the added line.

Recommended fix: Preserve evidence of edits outside the block across automatic rewrites. Use it to keep modifiedSince true and require discardLaterChanges when restoring the baseline would remove those edits. Add a save–edit–save–restore regression test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

.iter_mut()
.find(|set| set.id == id)
.ok_or_else(unknown_item)?;
if !set.text.contains(own) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Import drops distinct personal instructions

When a personal instruction matches only part of another sentence, import treats it as a duplicate. import_instructions then removes the original from the agent’s file without adding it to the library.

Learn more

Import moves text rather than copying it. import_instructions extracts the user's text, composes the updated library, and rewrites the site to contain only the managed block. A substring match does not establish that the library preserves the instruction as a standalone passage.

Example: Given the personal instruction Run tests. and a destination set containing Ignore this obsolete rule: Run tests., import removes the original line while leaving only the instruction to ignore it.

Recommended fix: Match complete passages with boundaries, such as the whole set or a complete paragraph-delimited passage, before deciding not to append. Test both a true duplicate and text embedded inside a different instruction.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1703 to +1705
let bytes = serde_json::to_vec_pretty(&file).map_err(|_| write_failed())?;
write_replacing(&config.directory.join(LIBRARY_FILE), &bytes)
.map_err(|_| write_failed())?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Import locks users out of the library

When an import pushes the library past 8 MiB, store_library commits a file load rejects. The import rewrites the source file and returns success, but users can no longer read or save the library.

Learn more

Library validation limits individual sets and per-agent text, not the total serialized size. import_instructions validates the new library and checks the generated instruction file, then replaces that file before saving the library. The new library can exceed 8 MiB even when the generated file is small, because most of the stored text need not apply to the importing agent. save also cannot read a library over that limit to check its revision.

Example: With a readable library just 8 KiB below the limit, import a 16 KiB instruction file into a new scoped set. The set and instruction file are both valid, but Core saves a library it cannot read back.

Recommended fix: Serialize the proposed library and enforce MAX_LIBRARY_FILE_BYTES before replacing the source instruction file. Reuse the validated bytes for the commit and apply the same size check to ordinary saves.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +790 to +791
let _ = write_replacing(&real, file.as_bytes());
backups::undo(taken);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Failed import deletes its recovery copy

When saving the library and restoring the original file both fail, import_instructions still discards the new backup. undo deletes the pre-import copy even though the agent’s file still holds the replacement.

Learn more

replace_file records a backup before installing the import's replacement. If saving the library fails, the import attempts to put the previous instruction file back. If that write fails too, removing the backup discards the copy of the exact pre-import bytes even though the replacement is still present. In particular, an edited block the user agreed to replace is not included in the imported text.

Example: A storage error stops both the library commit and the write that would restore the original instruction file. After a first change with replace: true, the original edited block exists only in the new backup. The failure path deletes that backup and reports only the library write failure.

Recommended fix: Undo the backup only after the original file has been restored successfully. On rollback failure, preserve the recovery copy and record the distinct failure in the unified log with a stable error category and no file content.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant